<?php

/**
 * 文件：api.php
 * 作者：mqycn
 * 博客：http://www.miaoqiyuan.cn
 * 源码：https://gitee.com/mqycn/ShortUrl
 * 说明：API接口
 */

require_once 'config.php';

$result = [
	'status' => false,
	'msg' => 'OK',
];

if (empty($_GET['appKey']) || $_GET['appKey'] != AppKey) {
	$result['msg'] = 'AppKey Error!';
} else {
	switch ($_GET['method']) {
	case 'install':
		$result['status'] = $db->install();
		break;
	default:
		$url = isset($_GET['url']) ? $_GET['url'] : '';
		if (preg_match('/^(http|https)\:\/\//', $url)) {
			$result['status'] = true;
			$result['url'] = AppRoot . $db->create($url);
		} else {
			$result['msg'] = 'Url Error!';
		}
		break;
	}
}

echo (json_encode($result));